<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="4">
              <a-form-item label="部门" prop="type">
                <a-tree-select
                  v-model="queryParam.deptId"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="deptOptions"
                  placeholder="请选择"
                  :replaceFields="{
                    children: 'children',
                    title: 'menuName',
                    key: 'id',
                    value: 'id',
                  }"
                  @change="onMenuTreeChange"
                  allow-clear
                >
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="客户经理">
                <a-select
                  placeholder="请选择客户经理"
                  v-model="queryParam.userId"
                  :getPopupContainer="
                    (triggerNode) => {
                      return triggerNode.parentNode || document.body;
                    }
                  "
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="d in userNamesList"
                    :key="d.name"
                    :value="d.id"
                    >{{ d.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="进件编号">
                <a-input
                  v-model="queryParam.jinjianId"
                  placeholder="请输入进件编号"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="客户姓名">
                <a-input
                  v-model="queryParam.cusName"
                  placeholder="请输入客户姓名"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="申请时间">
                <a-range-picker
                  style="width: 100%"
                  v-model="dateTimes"
                  valueFormat="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  allow-clear
                />
              </a-form-item>
            </a-col>
            <a-col :span="4" v-if="advanced">
              <a-form-item label="确认状态" prop="caiwuCheck">
                <a-select
                  placeholder="请选择结单状态"
                  v-model="queryParam.caiwuCheck"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in caiwuCheckOptions"
                    :key="index"
                    :value="d.dictValue"
                    >{{ d.dictLabel }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? "收起" : "展开" }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-jinjian-bill-add-form
        v-if="showAddModal"
        ref="bizJinjianBillAddForm"
        :isZuofeiOptions="isZuofeiOptions"
        :caiwuCheckOptions="caiwuCheckOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-jinjian-bill-edit-form
        v-if="showEditModal"
        ref="bizJinjianBillEditForm"
        :isZuofeiOptions="isZuofeiOptions"
        :caiwuCheckOptions="caiwuCheckOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="费用管理"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizJinjianBill-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizJinjianBillList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
        :scroll="{ x: '100%' }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizJinjianBill:export']"
          >
            <a-icon type="download" />导出
          </a-button>
          <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizJinjianBill:remove']"
          >
            <a-icon type="delete" />删除
          </a-button>
        </div>

        <span slot="isZuofei" slot-scope="{ record }">
          <a-tag color="green" v-if="record.isZuofei == 0">
            {{ isZuofeiFormat(record) }}
          </a-tag>
          <a-tag color="red" v-if="record.isZuofei == 1">
            {{ isZuofeiFormat(record) }}
          </a-tag>
        </span>

        <span slot="caiwuCheck" slot-scope="{ record }">
          <a-tag color="orange" v-if="record.caiwuCheck == 0">
            {{ caiwuCheckFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="record.caiwuCheck == 1">
            {{ caiwuCheckFormat(record) }}
          </a-tag>
        </span>
        <span slot="jinjianId" slot-scope="{ text }">
          <a>{{ text }}</a>
        </span>

        <span slot="status" slot-scope="{ record }">
          <a-tag color="orange" v-if="record.status == 0">
            {{ statusFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="record.status == 1">
            {{ statusFormat(record) }}
          </a-tag>
        </span>
        <span slot="numStr" slot-scope="{ text }">
          ¥ {{ formatNumber(text) }}
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <!-- <a-button
            type="link"
            icon="money-collect"
            @click="handleAdd(record)"
            v-if="record.caiwuCheck == 0"
            >财务确认</a-button
          > -->
          <a-button
            type="link"
            icon="money-collect"
            @click="handleUpdate(record)"
            v-if="record.caiwuCheck == 0"
            >确认</a-button
          >
          <a-button
            a-button
            type="link"
            icon="eye"
            style="color: green"
            v-if="record.caiwuCheck == 1"
            @click="handleUpdate(record)"
            >查看</a-button
          >
          <!-- <a-divider type="vertical" /> -->
          <!-- <a @click="handleUpdate(record)"> 结单 </a> -->
          <!-- <a-button type="link" icon="carry-out" @click="handleUpdate(record)"
            >结单</a-button
          > -->
          <!-- <a-divider type="vertical" />
          <a @click="handleDelete(record)"> 删除 </a> -->
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
      </advance-table>
    </a-card>

    <ant-modal
      modalWidth="1600"
      modalHeight="520"
      :visible="visible"
      modal-title="费用详情"
      :adjust-size="true"
      @cancel="handleCancel"
      ><a-form-model ref="form" slot="content">
        <biz-jinjian-bill-index :jinjianId="jinjianId" v-if="visible" />
      </a-form-model>
      <template slot="footer">
        <!-- <a-button @click="handleCancel"> 取消 </a-button> -->
        <a-button type="primary" @click="handleCancel"> 确 认 </a-button>
      </template>
    </ant-modal>
  </div>
</template>
<script>
import {
  listBizJinjianBill,
  delBizJinjianBill,
  exportBizJinjianBill,
} from "@/api/biz/bizJinjianBill";
import { listUser } from "@/api/system/user";
import { listDeptTree } from "@/api/system/dept";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizJinjianBillAddForm from "@/views/biz/jinjianbill/modules/BizJinjianBillAddForm";
import BizJinjianBillEditForm from "@/views/biz/jinjianbill/modules/BizJinjianBillEditForm";
import BizJinjianBillIndex from "@/views/biz/jinjianbill/indexDetails";
import AntModal from "@/components/pt/dialog/AntModal";
export default {
  name: "BizJinjianBill",
  components: {
    AdvanceTable,
    BizJinjianBillAddForm,
    BizJinjianBillEditForm,
    BizJinjianBillIndex,
    AntModal,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      visible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 费用管理表格数据
      bizJinjianBillList: [],
      // 是否作废字典
      isZuofeiOptions: [],
      // 财务确认字典
      caiwuCheckOptions: [],
      // 结单状态字典
      statusOptions: [],
      dateTimes: [],
      userNamesList: [],
      deptOptions: [],
      // 根据岗位删选员工
      postIds: ["gwzg", "rzgw", "qyjl", "sg"],
      isKefu: 2,
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: undefined,

        caiwuCheck: undefined,

        cusName: undefined,

        status: undefined,
      },
      jinjianId: "",
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "55px",

          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "进件编号",
          dataIndex: "jinjianId",
          ellipsis: true,
          align: "left",

          width: "190px",
          scopedSlots: { customRender: "jinjianId" },
        },
        {
          title: "客户姓名",
          dataIndex: "cusName",
          ellipsis: true,
          align: "center",
          width: "100px",
        },
        {
          title: "申请时间",
          dataIndex: "createTime",
          ellipsis: true,
          align: "center",
          width: "160px",
        },
        {
          title: "签单日期",
          dataIndex: "qdTime",
          ellipsis: true,
          align: "center",
          width: "130px",
        },

        {
          title: "放款金额(元)",
          dataIndex: "fkAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },
        {
          title: "服务费(元)",
          dataIndex: "serviceAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },
        {
          title: "回款金额(元)",
          dataIndex: "serviceConfirmAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },

        {
          title: "确认收入金额(元)",
          dataIndex: "serviceAmountCheck",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },

        {
          title: "财务确认",
          dataIndex: "caiwuCheck",
          scopedSlots: { customRender: "caiwuCheck" },
          align: "center",
          width: "100px",
        },

        {
          title: "客户经理",
          dataIndex: "userName",
          ellipsis: true,
          align: "center",
          width: "100px",
        },
        {
          title: "客服姓名",
          dataIndex: "serviceName",
          ellipsis: true,
          align: "center",
          width: "100px",
        },

        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          ellipsis: true,
          width: "100px",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("is_zuofei_status").then((response) => {
      this.isZuofeiOptions = response.data;
    });
    this.getDicts("caiwu_check_status").then((response) => {
      this.caiwuCheckOptions = response.data;
    });
    this.getDicts("jiedan_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDeptList();
  },

  methods: {
    getUserList(deptId) {
      const params = {
        pageNum: 1,
        pageSize: 100,
        deptId: deptId,
      };
      if (this.isKefu) {
        params.isKefu = this.isKefu;
      }
      if (this.postIds) {
        params.postIds = this.postIds;
      }
      listUser(params).then((res) => {
        this.userNamesList = res.data.list;
      });
    },
    onMenuTreeChange(value) {
      this.getUserList(value);
    },
    getDeptList() {
      listDeptTree("", 3).then((response) => {
        // this.expandTree(response.data, 3, this.expandedRowKeys);
        this.deptOptions = response.data;
      });
    },
    handleCancel() {
      this.visible = false;
      this.getList();
    },
    formatNumber(num) {
      return parseFloat(num || 0).toFixed(2);
    },
    /** 查询费用管理列表 */
    getList() {
      this.loading = true;

      listBizJinjianBill(
        this.addDateRange(this.queryParam, this.dateTimes)
      ).then((response) => {
        this.bizJinjianBillList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 是否作废字典翻译
    isZuofeiFormat(row) {
      if (row.isZuofei) {
        return this.selectDictLabel(this.isZuofeiOptions, row.isZuofei);
      } else {
        return "";
      }
    },
    // 财务确认字典翻译
    caiwuCheckFormat(row) {
      if (row.caiwuCheck) {
        return this.selectDictLabel(this.caiwuCheckOptions, row.caiwuCheck);
      } else {
        return "";
      }
    },
    // 结单状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: undefined,

        caiwuCheck: undefined,

        cusName: undefined,

        status: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleDetails(id) {
      this.visible = true;
      this.jinjianId = id;
    },
    handleAdd(record) {
      // if (record.caiwuCheck === "1") {
      //   this.$message.warning("请勿重复确认", 3);
      //   return;
      // }
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizJinjianBillAddForm.handleAdd(record));
    },
    handleUpdate(record, ids) {
      //   this.$message.warning("请勿重复结单", 3);
      //   return;
      // }
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizJinjianBillEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizJinjianBillIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizJinjianBill(bizJinjianBillIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizJinjianBill(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
